package cn.yhsx.jenkins.plugins.rotator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RotatorConfig {

    /**
     * 当前数据值索引
     */
    private Integer current;
    /**
     * 当前构建buildId
     */
    private String buildId;
    /**
     * 上次数据值对应成功构建的buildId。用来回滚
     * <p>
     * 非回滚构建成功，如果数据值切换会自动保存为上次构建的BuildId,其它则不变。
     */
    private String lastBuildId;
    private String range;
    private List<String> dataList;

    private Map<String, Map<String, String>> params;

    public RotatorConfig() {
    }

    public RotatorConfig(String range, Map<String, Map<String, String>> params) {

        this.range = range;
        this.current = 0;
        this.dataList = new ArrayList<>();
        this.params = new HashMap<>();
        String prefix = "", suffix = "";
        if (range.contains("[") && range.contains("]")) {
            prefix = range.substring(0, range.indexOf("["));
            suffix = range.substring(range.lastIndexOf("]") + 1);
            range = range.substring(range.indexOf("[") + 1, range.lastIndexOf("]"));
        }
        if (range.contains(",")) {
            for (String data : range.split(",")) {
                add(prefix + data + suffix, params);
            }
        } else {
            int min = 1, max;
            if (range.contains("~")) {
                String[] rs = range.split("~");
                min = Integer.parseInt(rs[0]);
                max = Integer.parseInt(rs[1]);
            } else {
                max = Integer.parseInt(range);
            }

            for (; min <= max; min++) {
                String data = prefix + min + suffix;
                add(data, params);
            }
        }
    }

    private void add(String data, Map<String, Map<String, String>> configParams) {
        dataList.add(data);
        Map<String, String> param = new HashMap<>();
        if (configParams != null && configParams.get(data) != null) {
            param = configParams.get(data);
        }
        params.put(data, param);
    }

    public Integer getCurrent() {
        return current;
    }

    public void setCurrent(Integer current) {
        this.current = current;
    }

    public String getRange() {
        return range;
    }

    public void setRange(String range) {
        this.range = range;
    }

    public List<String> getDataList() {
        return dataList;
    }

    public void setDataList(List<String> dataList) {
        this.dataList = dataList;
    }

    public Map<String, Map<String, String>> getParams() {
        return params;
    }

    public void setParams(Map<String, Map<String, String>> params) {
        this.params = params;
    }

    public String getBuildId() {
        return buildId;
    }

    public void setBuildId(String buildId) {
        this.buildId = buildId;
    }

    public String getLastBuildId() {
        return lastBuildId;
    }

    public void setLastBuildId(String lastBuildId) {
        this.lastBuildId = lastBuildId;
    }

    // 参数操作便捷方法
    public boolean addParameter(String key, String value) {
        Map<String, String> currentParams = getCurrentParams();
        String old = currentParams.get(key);
        if (old == null || !old.equals(value)) {
            currentParams.put(key, value);
            return true;
        }
        return false;
    }

    public String getCurrentData() {
        return dataList.get(current);
    }

    public Map<String, String> getCurrentParams() {
        return params.computeIfAbsent(String.valueOf(getCurrentData()), (o) -> new HashMap<>());
    }

}